package com.xebest.web.controller.vensys;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.common.util.XeRedisUtil;
import com.xebest.constant.XeConstans;
import com.xebest.constant.XeRedisKey;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.web.form.XeVenSysForm;
import com.xebest.web.model.XeVenSysEntity;
import com.xebest.web.service.XeVenSysParamService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenSysView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统参数中心
 *
 * @author 王亚萍
 */
@RestController
@RequestMapping("venSysParam")
public class XeVenSysParamController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenSysParamController.class);//日志记录类

    @Autowired
    private XeVenSysParamService xeVenSysParamService; //系统参数设置权限操作
    @Autowired
    protected RedisTemplate<String, String> redisTemplate;

    /**
     * 系统参数设置List
     *
     * @author 王亚萍
     */
    @RequestMapping("querySysParam")
    public @ResponseBody
    BaseResponse querySysParam() {
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        Map<String, Object> map = new HashMap<String, Object>();
        String result = "";
        try {
            //判断redis中是否有数据
            result = XeRedisUtil.redisUtil.hget(XeRedisKey.XE_ERP_SYSPARAMS_MAP_KEY, XeConstans.SYSPARAM_PREFIX + userInfo.getUserCode());
            if (null != result) {
                map.put("sysParamList", JSONArray.parse(result));
                return sendAjaxOK(map);
            }
            List<XeVenSysView> entity = xeVenSysParamService.querySysParam(userInfo.getUserCode());
            map.put("sysParamList", entity);

        } catch (Exception e) {

            // 输出结果
            List<XeVenSysView> entity = xeVenSysParamService.querySysParam(userInfo.getUserCode());
            map.put("sysParamList", entity);
        }
        return sendAjaxOK(map);

    }

    /**
     * 系统参数设置修改
     *
     * @author 王亚萍
     */
    @RequestMapping("updateSysFlagBySysType")
    public @ResponseBody
    BaseResponse updateSysFlagBySysType(@RequestBody XeVenSysForm form) {
        // 1、验证参数
        if (null == form || form.getList().size() == XeConstans._XE_ZERO)
            return sendAjaxError(BASEMessage.PARAM_ERROR);
        //获取用户登录信息
        ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
        try {
            int result = 0;
            // 根据USERCODE查询是否有该数据
            List<XeVenSysView> list = xeVenSysParamService.querySysParambyUserCode(userInfo.getUserCode());
            if (list.size() == 0) {
                for (int i = 0; i < form.getList().size(); i++) {
                    XeVenSysEntity entity1 = form.getList().get(i);
                    entity1.setUserCode(userInfo.getUserCode());
                    entity1.setCreateUser(userInfo.getLoginName());
                    result = this.xeVenSysParamService.insertSysParam(entity1);
                }
            } else {
                for (int i = 0; i < form.getList().size(); i++) {
                    XeVenSysEntity entity1 = form.getList().get(i);
                    entity1.setUserCode(userInfo.getUserCode());
                    entity1.setModifyUser(userInfo.getLoginName());
                    result = this.xeVenSysParamService.updateSysFlagBySysType(entity1);
                }
            }
            if (result > 0) {
                // 根据USERCODE查询到修改后的数据   然后存入缓存
                List<XeVenSysView> entity = xeVenSysParamService.querySysParambyUserCode(userInfo.getUserCode());
                XeRedisUtil.redisUtil.hset(XeRedisKey.XE_ERP_SYSPARAMS_MAP_KEY, XeConstans.SYSPARAM_PREFIX + userInfo.getUserCode(), JSON.toJSONString(entity));
            }
            return sendAjaxOK();
        } catch (Exception e) {
            log.error("系统参数设置异常：{}", e);
            return sendAjaxError();
        }
    }

    /**
     * 系统参数设置List  根据userCode和sysType查询
     *
     * @author 王亚萍
     */
    @RequestMapping("querySysParamBySysType")
    public @ResponseBody
    BaseResponse querySysParamBySysType(@RequestBody XeVenSysEntity form) {
        try {
            // 验证参数
            if (null == form || null == form.getSysType())
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            //获取用户登录信息
            Map<String, Object> map = new HashMap<>();
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            form.setSysType(form.getSysType());
            // 输出结果
            List<XeVenSysView> entity = xeVenSysParamService.querySysParamBySysType(form);
            map.put("sysParamList", entity);
            return sendAjaxOK(map);
        } catch (Exception e) {
            log.error("系统参数设置查询List异常：{}", e);
            return sendAjaxError();
        }
    }

}
  
